package com.yiyuan.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.yiyuan.common.base.BaseAction;
import com.yiyuan.common.exception.SubRecExistException;
import com.yiyuan.system.model.PriResource;
import com.yiyuan.system.object.TreeObj;
import com.yiyuan.system.service.ISystemService;

public class ResAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	/* ************* 参数封装******************************************** */

	private String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	/* ************* 输出结果封装***************************************** */
	private PriResource priResource;
	private List<PriResource> list;

	/* ************* 各功能函数******************************************* */
	// 资源引用
	@Resource
	private ISystemService systemService;

	public List<PriResource> getList() {
		return list;
	}

	public PriResource getPriResource() {
		return priResource;
	}

	public void setPriResource(PriResource priResource) {
		this.priResource = priResource;
	}

	// 进入资源主页
	public String index() {
		return "toMain";
	}

	// 保存权限资源
	public void save() {
		PriResource priResource = null;
		String retFlag = null;
		try {
			if (this.operation != null && this.operation.equals("add")) {// 新增权限资源
				priResource = new PriResource();
				priResource.setResName(this.resName);
				priResource.setResPath(this.resPath);
				priResource.setResType(this.resType);
				priResource.setResDesc(this.resDesc);
				priResource.setResPid(Integer.valueOf(this.resId));
				priResource.setHasChild("0");

				this.systemService.addPriResource(priResource);
				retFlag = "add";
			} else if (this.operation != null
					&& this.operation.equals("update")) {// 修改权限资源
				priResource = this.systemService.loadPriResource(Integer
						.valueOf(this.resId));
				priResource.setResName(this.resName);
				priResource.setResPath(this.resPath);
				priResource.setResType(this.resType);
				priResource.setResDesc(this.resDesc);
				this.systemService.updatePriResource(priResource);
				retFlag = "update";
			} else {
				retFlag = "error";
			}
		} catch (Exception e) {
			e.printStackTrace();
			retFlag = "error";
		}
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(retFlag);
		} catch (Exception e) {

		}
	}

	// 获得权限资源
	public void load() {
		priResource = systemService.loadPriResource(Integer.valueOf(id));
		JSONObject jso = new JSONObject(priResource);
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter()
					.print(jso.toString());
		} catch (Exception e) {

		}
	}

	// 查询权限资源树
	@SuppressWarnings("unchecked")
	public void listTree() {

		TreeObj treeObj = null;
		PriResource priResource = null;
		List<TreeObj> treeList = new ArrayList();
		if (id == null || id.equals("")) {
			id = "0";
		}
		list = systemService.findSubPriResList(id);
		for (int i = 0; i < list.size(); i++) {
			priResource = list.get(i);
			treeObj = new TreeObj();
			treeObj.setId(priResource.getResId().toString());
			treeObj.setPid(priResource.getResPid().toString());
			treeObj.setText(priResource.getResName());
			treeObj.setCheckstate(0);
			treeObj.setChildNodes(null);
			treeObj.setComplete(false);
			if (priResource.getHasChild() != null
					&& priResource.getHasChild().equals("1")) {
				treeObj.setHasChildren(true);
			} else {
				treeObj.setHasChildren(false);
			}
			treeObj.setIsexpand(false);
			treeObj.setShowcheck(true);
			treeObj.setValue(priResource.getResId().toString());
			treeList.add(treeObj);

		}
		Map map = new HashMap();
		map.put("list", treeList);
		JSONObject jso = new JSONObject(map);
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter()
					.print(jso.toString());
		} catch (Exception e) {
		}
	}

	// 删除机构
	public void del() {
		String ret = null;
		try {
			this.systemService.deletePriResource(Integer.valueOf(this.id));
			ret = "delete";
		} catch (SubRecExistException se) {
			ret = "exist";
		} catch (Exception e) {
			ret = "error";
		}

		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(ret);
		} catch (Exception e) {

		}

	}

	/* **************表单参数******************************************** */
	private String resId;
	private String resPid;
	private String resName;
	private String resPath;
	private String resType;
	private String resDesc;
	private String operation;

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public String getResId() {
		return resId;
	}

	public void setResId(String resId) {
		this.resId = resId;
	}

	public String getResPid() {
		return resPid;
	}

	public void setResPid(String resPid) {
		this.resPid = resPid;
	}

	public String getResName() {
		return resName;
	}

	public void setResName(String resName) {
		this.resName = resName;
	}

	public String getResPath() {
		return resPath;
	}

	public void setResPath(String resPath) {
		this.resPath = resPath;
	}

	public String getResType() {
		return resType;
	}

	public void setResType(String resType) {
		this.resType = resType;
	}

	public String getResDesc() {
		return resDesc;
	}

	public void setResDesc(String resDesc) {
		this.resDesc = resDesc;
	}

}
